package com.aruba.canvasapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.graphics.Canvas.ALL_SAVE_FLAG;

/**
 * 演示Canvas clip的View
 */
public class RevealView extends View {
    private Paint mPaint = new Paint();
    private Map<Integer, List<Bitmap>> bitmap = new HashMap<>();
    private float needClipWidth, needClipHeight;
    private float maxWidth, maxHeight;
    private float translateX;
    private float dx;
    private float currentX;

    public RevealView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private void init() {
        if (bitmap.size() != 0) return;

        mPaint.setDither(true);

        List<Bitmap> bitmaps1 = new ArrayList<>();
        bitmaps1.add(BitmapFactory.decodeResource(getResources(), R.drawable.avft));
        bitmaps1.add(BitmapFactory.decodeResource(getResources(), R.drawable.avft_active));
        bitmap.put(1, bitmaps1);

        List<Bitmap> bitmaps2 = new ArrayList<>();
        bitmaps2.add(BitmapFactory.decodeResource(getResources(), R.drawable.box_stack));
        bitmaps2.add(BitmapFactory.decodeResource(getResources(), R.drawable.box_stack_active));
        bitmap.put(2, bitmaps2);

        List<Bitmap> bitmaps3 = new ArrayList<>();
        bitmaps3.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubble_frame));
        bitmaps3.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubble_frame_active));
        bitmap.put(3, bitmaps3);

        List<Bitmap> bitmaps4 = new ArrayList<>();
        bitmaps4.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubbles));
        bitmaps4.add(BitmapFactory.decodeResource(getResources(), R.drawable.bubbles_active));
        bitmap.put(4, bitmaps4);

        List<Bitmap> bitmaps5 = new ArrayList<>();
        bitmaps5.add(BitmapFactory.decodeResource(getResources(), R.drawable.bullseye));
        bitmaps5.add(BitmapFactory.decodeResource(getResources(), R.drawable.bullseye_active));
        bitmap.put(5, bitmaps5);

        List<Bitmap> bitmaps6 = new ArrayList<>();
        bitmaps6.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_filled));
        bitmaps6.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_filled_active));
        bitmap.put(6, bitmaps6);

        List<Bitmap> bitmaps7 = new ArrayList<>();
        bitmaps7.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_outline));
        bitmaps7.add(BitmapFactory.decodeResource(getResources(), R.drawable.circle_outline_active));
        bitmap.put(7, bitmaps7);

        //最大宽高
        maxWidth = bitmaps1.get(0).getWidth() * 7;
        maxHeight = bitmaps1.get(0).getHeight();
        if (maxWidth > getMeasuredWidth()) {//缩放
            float scaleX = getMeasuredWidth() / maxWidth;
            for (int i = 1; i <= 7; i++) {
                Bitmap newBitmap1 = Bitmap.createScaledBitmap(bitmap.get(i).get(0), (int) (bitmap.get(i).get(0).getWidth() * scaleX), (int) (bitmap.get(i).get(0).getHeight() * scaleX), true);
                bitmap.get(i).set(0, newBitmap1);
                Bitmap newBitmap2 = Bitmap.createScaledBitmap(bitmap.get(i).get(1), (int) (bitmap.get(i).get(1).getWidth() * scaleX), (int) (bitmap.get(i).get(1).getHeight() * scaleX), true);
                bitmap.get(i).set(1, newBitmap2);
            }

            maxWidth = getMeasuredWidth();
            maxHeight = maxHeight * scaleX;
        }
        //需要裁剪的宽高
        needClipWidth = maxWidth / 7;
        needClipHeight = maxHeight;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                dx = 0;
                currentX = event.getX();
                return true;
            case MotionEvent.ACTION_MOVE:
                //记录偏移量
                dx = event.getX() - currentX;
                currentX = event.getX();
                postInvalidate();
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        init();

        //先画有灰色的
        int nowLeft = 0;
        for (int i = 1; i <= 7; i++) {
            canvas.drawBitmap(bitmap.get(i).get(0), nowLeft, 0, mPaint);
            //偏移
            nowLeft += bitmap.get(i).get(0).getWidth();
        }

        int layerCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, ALL_SAVE_FLAG);

        translateX += dx;
        //平移操作达到最右边或着最左边，停止
        if (translateX <= -(maxWidth / 2 - needClipWidth / 2)) {
            translateX = -(maxWidth / 2 - needClipWidth / 2);
        }
        if (translateX >= maxWidth / 2 - needClipWidth / 2) {
            translateX = maxWidth / 2 - needClipWidth / 2;
        }

        //将画布裁剪
        canvas.clipRect(new RectF(maxWidth / 2 - needClipWidth / 2 + translateX, 0, maxWidth / 2 + needClipHeight / 2 + translateX, needClipHeight));

        //再画有色彩的
        nowLeft = 0;
        for (int i = 1; i <= 7; i++) {
            canvas.drawBitmap(bitmap.get(i).get(1), nowLeft, 0, mPaint);
            //偏移
            nowLeft += bitmap.get(i).get(1).getWidth();
        }

        canvas.restoreToCount(layerCount);
    }
}
